// distribution/moblie/mobile.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

// 引入tDesign消息提示组件
import Message from 'tdesign-miniprogram/message/index';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,

        styledata: {
            "bg_color": "white",
            "color": "#000",
            "leftflag": true,
            // "homeflag": true,
            "name": "手机绑定"
        },

        moblieVal: "",
        inputLen: 6,
        iptValue: "",
        isFocus: false,

        // 发送验证码按钮
        isForbid: false,
        textfont: `发送验证码`,

        // 验证码聚焦标识
        codefocus: false,

        uid: '',
        fullUserInfo: {},
        isVip: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.init_data()
    },

    init_data() {
        this.setData({
            uid: App.globalData.uid,
            // uid: 7486,

            fullUserInfo: App.globalData.fulluserInfo,
            isVip: App.globalData.isVip
        })

        let uid = this.data.uid
        if (!uid) {
            util.showToast("您未登录")
        }
    },

    // 手机号输入获取焦点,验证框失去焦点
    moblieFocus() {
        this.setData({
            codefocus: false
        })
    },
    // 手机号输入失去焦点,验证框获取焦点
    moblieBlur() {
        this.setData({
            codefocus: true
        })
    },

    onFocus: function (e) {
        this.setData({ isFocus: true });
    },
    setValue: function (e) {
        this.setData({ iptValue: e.detail.value });
    },
    setMoblieVal: function (e) {
        this.setData({ moblieVal: e.detail.value });
    },

    // 手机号正则匹配验证
    checkTelephone(tel) {
        /* 手机号校验规则*/
        const telCheck = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        return telCheck.test(tel)
    },

    async sendCode() {
        let moblie = this.data.moblieVal
        console.log(moblie);
        if (!this.checkTelephone(moblie)) {
            return util.showToast("请输入正确的手机号")
        }

        // 聚焦到验证码输入框
        this.onFocus()
        let params = {
            uid: this.data.uid,
            // uid: "",
            moblie
        }

        let result = await request.postall(api.applymoblie, params)
        console.log(result);
        if (result.code == 0) {
            util.showSuccess(result.msg)

            let num = 60
            this.setData({
                isForbid: true,
                textfont: `重新发送${num}`
            })
            this.changetip(num)

        } else {
            util.showFail(result.msg)
        }
    },

    // 倒数提示
    changetip(num = 60) {
        let timer = setInterval(() => {
            if (num == 1) {
                this.setData({
                    isForbid: false,
                    textfont: `发送验证码`
                })
                clearInterval(timer)
                return
            }

            num--
            this.setData({
                textfont: `重新发送${num}`
            })
        }, 1000);
    },

    // 粘贴事件
    presshandle() {
        console.log("长按");
        let that = this
        wx.getClipboardData({
            success: function (res) {
                // 隐藏"xx读取了你的剪切板内容"
                wx.hideToast()
                let num = parseInt(res.data)
                if (typeof num == "number" && !isNaN(num)) {
                    console.log('剪贴板内容是：' + num);
                    that.setData({
                        iptValue: res.data
                    })
                } else {
                    util.showToast("请粘贴数字验证码")
                }
            }
        })
    },

    // 验证6位数字验证码
    isValidCode(code) {
        return /^\d{6}$/.test(code);
    },

    // 发送验证码到后台绑定手机
    async confirmcode() {
        let code = this.data.iptValue
        let moblie = this.data.moblieVal

        console.log(code);
        if (!this.isValidCode(code)) {
            return util.showToast("验证码格式有误")
        }

        let params = {
            uid: this.data.uid,
            code,
            moblie
        }

        let result = await request.postall(api.bindmoblie, params)
        // console.log(result);
        if (result.code == 0) {
            // 关闭当前页面
            let tip = "手机号绑定成功 恭喜您成为金顶分销员！"
            this.showSuccessMessage(tip)
            let timer = setTimeout(() => {
                clearTimeout(timer)
                wx.redirectTo({
                    url: '/distribution/index/index',
                    success: (result) => {

                    },
                    fail: () => { },
                    complete: () => { }
                });
            }, 2500);

        } else {
            this.showErrorMessage(result.msg)
            return
        }
    },

    // 错误提示
    showErrorMessage(content) {
        Message.error({
            context: this,
            offset: [144, 130],
            duration: 2500,
            content: content,
        });
    },
    // 成功提示
    showSuccessMessage(content) {
        Message.success({
            context: this,
            offset: [144, 130],
            duration: 2500,
            content: content,
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})